poc: [For Evaluation] example implementation of assisted injection - #127
poc: [For Evaluation] example implementation of assisted injection#127kalymero wants to merge 2 commits into
Conversation
|
Hello, in my opnion, this approach creates a tight coupling between the IoC container and object creation. Instead of injecting a function with dynamic parameters, why not register a factory in the container? That way, the container resolves the factory, and the factory handles object creation with the required parameters. This keeps the DI setup cleaner and avoids unnecessary complexity. for example or you can register a builder |
|
Hi @juarezfranco, thanks. Dart limitations don't help here, but it would be nice to pass a list of typed parameters instead of a map. I couldn't figure out a nice way of doing it with Dart 2 Another possible Dart-ish approach could be to have the container to generate the Factory class based on annotations, like for instance Guice does
|
to keep typesafety
|
@gbtb16 have pushed a commit to show how, updating to Dart >= 3.0.0, the Records can be leveraged to keep typesafety. In particular then, if you want assisted injection, you may:
typedef SithAssistedParameters = ({String id});
container.registerAssistedFactory<Sith, SithAssistedParameters>((c) {
return (SithAssistedParameters args) =>
Sith('Sheev', 'Palpatine', args.id);
});
final Sith sith =
container.resolve<AssistedInjector<Sith, SithAssistedParameters>>()(
(id: 'DarthSidious')); |
Hi Gabriel,
Kiwi is a very simple and beautiful DI framework, congrats!
I think a missing key feature is assisted injection, so here i've put together an example implementation for you to evaluate.
Another option would be using Records instead of Maps for the arguments, but it will require the bump of the SDK version to at least > 3.0.0
Let me know how you feel about it and if it makes sense for you.